package com.jeff.tianti.common.dto;

import com.jeff.tianti.common.entity.PageModel;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.domain.Page;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public class AjaxResult {

    private boolean success;

    private String msg;

    private Object data;
    private Integer code;

    public Integer getCode() {
        if (code == null) {
            code = 200;
        }
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public boolean isSuccess() {
        return success;
    }

    public void setSuccess(boolean success) {
        this.success = success;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = data;
    }

    public static AjaxResult setSuccessData(Object data) {
        AjaxResult ajaxResult = new AjaxResult();
        ajaxResult.setCode(200);
        ajaxResult.setSuccess(true);
        ajaxResult.setMsg("成功");
        ajaxResult.setData(data);
        return ajaxResult;
    }

    public static AjaxResult setSuccessPageDate(Page pageDate) {
        AjaxResult ajaxResult = new AjaxResult();
        Map dataMap = new HashMap<>();
        dataMap.put("totalPages",pageDate.getTotalPages());
        dataMap.put("totalSize",pageDate.getTotalElements());
        dataMap.put("data",pageDate.getContent());
        ajaxResult.setCode(200);
        ajaxResult.setSuccess(true);
        ajaxResult.setMsg("成功");
        ajaxResult.setData(dataMap);
        return ajaxResult;
    }
    public static AjaxResult setSuccessPageModelDate(PageModel pageModel) {
        AjaxResult ajaxResult = new AjaxResult();
        Map dataMap = new HashMap<>();
        dataMap.put("totalPages",pageModel.getTotalPage());
        dataMap.put("totalSize",pageModel.getTotalCount());
        dataMap.put("data", pageModel.getList() == null ? new ArrayList<>() : pageModel.getList());
        ajaxResult.setCode(200);
        ajaxResult.setSuccess(true);
        ajaxResult.setMsg("成功");
        ajaxResult.setData(dataMap);
        return ajaxResult;
    }

    public static AjaxResult setSuccessPageDate(Object data,int totalPages,long totalSize) {
        AjaxResult ajaxResult = new AjaxResult();
        Map dataMap = new HashMap<>();
        dataMap.put("totalPages",totalPages);
        dataMap.put("totalSize",totalSize);
        dataMap.put("data",data);
        ajaxResult.setCode(200);
        ajaxResult.setSuccess(true);
        ajaxResult.setMsg("成功");
        ajaxResult.setData(dataMap);
        return ajaxResult;
    }

}
